#include <Arduino_BuiltIn.h>

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

//全局变量
int i=0;
bool up=true;
bool isManual=false;
String reciveStr="";
bool serial_read_complete=false;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  reciveStr.reserve(200);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  while(!Serial){}
  Serial.println("Board start,and led on board is auto blink;");
  mainMenu();
}

// the loop fzunction runs over and over again forever
void loop() {
  if (serial_read_complete) {
    Serial.println();
    Serial.println("Reciving commond:["+reciveStr+"]");
    if(reciveStr.equals("1")){
      Serial.println("Led is auto blink like breath");
      ledOpen();
      Serial.println();
      isManual = false;
    }else if(reciveStr.equals("2")){

    }else{
      Serial.println("You sented wrong commond,Please retry!");
      Serial.println();
      mainMenu();
    }

    //清空读取内容
    reciveStr="";
    //清空串口读取状态
    serial_read_complete = false;
  }
  if(isManual){
    //手动模式
    
  }else{
    autoBlink();
  }
}

void mainMenu(){
  Serial.println("Print the following item number to control it:");
  Serial.println("1 Auto blink");
  Serial.println("2 Manual control");
  Serial.println("Please input command:");
}

//串口中断，当串口接收到数据的时候执行
void serialEvent(){
  while(Serial.available()>0){
    char recive_bytes=(char)Serial.read();
    if(recive_bytes != '\n'){
      reciveStr += recive_bytes;
    }else{
      serial_read_complete = true;
    }
  }
}

void autoBlink(){
  if(up){
    i++;
    if(i>=25){
      up=false;
    }
  }else{
    i--;
    if(i<=0){
      up=true;
      delay(2000);
    }
  }
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(i);                         // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(25-i);                      // wait for a second
}

void ledOpen(){
  digitalWrite(LED_BUILTIN, LOW);
  int i=0;
  while(i<4){
    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(50);                         // wait for a second
    digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
    delay(50);
    i++;
  }
  i=0;
}
